package com.charles.video.system.service.impl;

import com.charles.video.api.domain.SysUser;
import com.charles.video.common.core.exception.ServiceException;
import com.charles.video.common.core.utils.BcryptUtils;
import com.charles.video.common.core.utils.StringUtils;
import com.charles.video.system.mapper.SysUserMapper;
import com.charles.video.system.service.SysUserService;
import com.charles.video.system.vo.UserModifyForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @author Charles-H
 * <p>
 * 用户业务接口实现
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Value("${cc.video.default.avatar}")
    private String defaultAvatar;

    @Override
    public SysUser validateAddUser(SysUser user) {
        String account = user.getAccount();
        String password = user.getPassword();
        String email = user.getEmail();

        if (StringUtils.isAnyBlank(account, password, email)) {
            throw new ServiceException("新增用户信息不完善");
        }

        if (!account.matches("^[a-z]\\d{10,15}$")) {
            throw new ServiceException("新增用户帐号格式不规范");
        }

        if (!password.matches("^[a-zA-Z0-9]{6,10}$")) {
            throw new ServiceException("新增用户密码格式不规范");
        }

        if (!email.matches("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+$")) {
            throw new ServiceException("新增用户邮箱格式不规范");
        }

        if (sysUserMapper.queryUserByAccount(account) != null) {
            throw new ServiceException("新增用户帐号已存在");
        }

        if (sysUserMapper.queryUserByEmail(email) != null) {
            throw new ServiceException("新增用户邮箱已存在");
        }

        user.setNickname("用户" + account);
        user.setPassword(BcryptUtils.bcryptPassword(password));
        user.setAvatar(defaultAvatar);
        return user;
    }

    @Override
    public UserModifyForm validateModifyUser(UserModifyForm user) {
        String account = user.getAccount();
        String password = user.getPassword();
        String nickname = user.getNickname();
        String email = user.getEmail();

        if (StringUtils.isEmpty(account) || StringUtils.isEmpty(password) || StringUtils.isEmpty(email)) {
            throw new ServiceException("修改用户信息不完善");
        }

        if (!account.matches("^[a-z]\\d{10,15}$")) {
            throw new ServiceException("修改用户帐号格式不规范");
        }

        if (nickname.length() < 2 || nickname.length() > 20) {
            throw new ServiceException("修改用户昵称格式不规范");
        }

        if (!email.matches("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+$")) {
            throw new ServiceException("修改用户邮箱格式不规范");
        }

        SysUser u1 = sysUserMapper.queryUserByAccount(account);
        if (u1 != null && !u1.getUser_id().equals(user.getUserId())) {
            throw new ServiceException("修改用户帐号已存在");
        }

        SysUser u2 = sysUserMapper.queryUserByEmail(email);
        if (u2 != null && !u2.getUser_id().equals(user.getUserId())) {
            throw new ServiceException("修改用户邮箱已存在");
        }

        if (!sysUserMapper.queryUserModifyForm(user.getUserId()).getPassword().equals(password)) {
            if (!password.matches("^[a-zA-Z0-9]{6,10}$")) {
                throw new ServiceException("修改用户密码格式不规范");
            }
            user.setPassword(BcryptUtils.bcryptPassword(password));
        }

        return user;
    }
}
